﻿using CQRS.Events;
using CQRS.Example.SampleApplication.Views;
using CQRS.Sample.Domain.BOs;
using Castle.MicroKernel.Registration;
using Castle.Windsor;

namespace CQRS.Example.SampleApplication
{
    public class WindowFormInstaller
    {
        public static void Install(IWindsorContainer container)
        {
            container.Register(FindWindowsForms().Configure(x => x.LifestyleTransient()));

            container.Register(FindCommandHandlers().Configure(x => x.LifestyleTransient()));

            container.Register(FindEventHandlers().Configure(x => x.LifestyleTransient()));
        }

        private static BasedOnDescriptor FindEventHandlers()
        {
            return AllTypes.FromAssemblyContaining(typeof(Plane)).BasedOn(typeof(IEventHandler<>));
        }
        private static BasedOnDescriptor FindCommandHandlers()
        {
            return AllTypes.FromAssemblyContaining(typeof(Plane)).BasedOn(typeof(ICommandHandler<>));
        }
        private static BasedOnDescriptor FindWindowsForms()
        {
            return AllTypes.FromThisAssembly().BasedOn<BaseForm>()
                .If(t => t.Name.EndsWith("Form"));
        }
    }
}